<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_PhotoController extends Core_Controller_Action_Standard {
	public function init() {
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			if (0 !== ($photo_id = ( int ) $this->_getParam ( 'photo_id' )) && null !== ($photo = Engine_Api::_ ()->getItem ( 'cmspage_photo', $photo_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $photo );
			} 

			else if (0 !== ($cmspage_id = ( int ) $this->_getParam ( 'cmspage_id' )) && null !== ($cmspage = Engine_Api::_ ()->getItem ( 'cmspage', $cmspage_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $cmspage );
			}
		}
		
		$this->_helper->requireUser->addActionRequires ( array ('upload', 'upload-photo', 'edit', 'delete', 'manage' ) );
		
		$this->_helper->requireSubject->setActionRequireTypes ( array ('list' => 'cmspage', 'upload' => 'cmspage', 'view' => 'cmspage_photo', 'edit' => 'cmspage_photo', 'delete' => 'cmspage_photo', 'manage' => 'cmspage' ) );
	}
	
	public function listAction() {
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ();
		$this->view->album = $album = $cmspage->getSingletonAlbum ();
		
		$this->view->paginator = $paginator = $album->getCollectiblesPaginator ();
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->owner = $owner = Engine_Api::_ ()->getItem ( 'user', $cmspage->owner_id );
		
		$this->view->canUpload = $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'photo' )->checkRequire ();
		
		$album->view_count ++;
		$album->save ();
	}
	
	public function manageAction() {
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ();
		
		if (! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'photo' )->isValid ())
			return;
		
		//echo 'stupid';
		//if( !$this->_helper->requireUser()->isValid() ) return;
		//if( !$this->_helper->requireAuth()->setAuthParams($cmspage, null, 'edit')->isValid() ) return;
		

		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		//$this->view->canUpload = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'cmspage', 'photo');
		

		// Prepare data
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ();
		$this->view->album = $album = $cmspage->getSingletonAlbum ();
		$this->view->paginator = $paginator = $album->getCollectiblesPaginator ();
		
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		$paginator->setItemCountPerPage ( $paginator->getTotalItemCount () );
		
		// Make form
		$this->view->form = $form = new Cmspage_Form_Photo_Manage ();
		
		//$form->addNotice(Zend_Registry::get('Zend_Translate')->_('Your changes were saved.'));
		

		foreach ( $paginator as $photo ) {
			$subform = new Cmspage_Form_Photo_Manage_Edit ( array ('elementsBelongTo' => $photo->getGuid () ) );
			$subform->populate ( $photo->toArray () );
			$form->addSubForm ( $subform, $photo->getGuid () );
			//$form->cover->addMultiOption($photo->getIdentity(), $photo->getIdentity());
			$form->cover->addMultiOption ( $photo->file_id, $photo->file_id );
		}
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		$table = $cmspage->getTable ();
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$values = $form->getValues ();
			if (! empty ( $values ['cover'] )) {
				$cmspage->photo_id = $values ['cover'];
				$cmspage->save ();
			}
			
			// Process
			foreach ( $paginator as $photo ) {
				$subform = $form->getSubForm ( $photo->getGuid () );
				$values = $subform->getValues ();
				
				$values = $values [$photo->getGuid ()];
				unset ( $values ['photo_id'] );
				if (isset ( $values ['delete'] ) && $values ['delete'] == '1') {
					$photo->delete ();
				} else {
					$photo->setFromArray ( $values );
					$photo->save ();
				}
			}
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		return $this->_helper->redirector->gotoRoute ( array ('controller' => 'photo', 'action' => 'list', 'subject' => $cmspage->getGuid () ), 'cmspage_extended', true );
	
	}
	
	public function uploadAction() {
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ();
		if (isset ( $_GET ['ul'] ) || isset ( $_FILES ['Filedata'] ))
			return $this->_forward ( 'upload-photo', null, null, array ('format' => 'json', 'cmspage_id' => ( int ) $cmspage->getIdentity () ) );
		
		if (! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'photo' )->isValid ())
			return;
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$album = $cmspage->getSingletonAlbum ();
		
		$this->view->cmspage_id = $cmspage->cmspage_id;
		$this->view->form = $form = new Cmspage_Form_Photo_Upload ();
		$form->file->setAttrib ( 'data', array ('cmspage_id' => $cmspage->getIdentity () ) );
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		$table = Engine_Api::_ ()->getItemTable ( 'cmspage_photo' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$values = $form->getValues ();
			$params = array ('cmspage_id' => $cmspage->getIdentity (), 'user_id' => $viewer->getIdentity () );
			
			// Add action and attachments
			$api = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' );
			$action = $api->addActivity ( Engine_Api::_ ()->user ()->getViewer (), $cmspage, 'cmspage_photo_upload', null, array ('count' => count ( $values ['file'] ) ) );
			
			// Do other stuff
			$count = 0;
			foreach ( $values ['file'] as $photo_id ) {
				$photo = Engine_Api::_ ()->getItem ( "cmspage_photo", $photo_id );
				if (! ($photo instanceof Core_Model_Item_Abstract) || ! $photo->getIdentity ())
					continue;
				
				$photo->collection_id = $album->album_id;
				$photo->album_id = $album->album_id;
				$photo->save ();
				
				if ($cmspage->photo_id == 0) {
					$cmspage->photo_id = $photo->file_id;
					$cmspage->save ();
				}
				
				if ($action instanceof Activity_Model_Action && $count < 8) {
					$api->attachActivity ( $action, $photo, Activity_Model_Action::ATTACH_MULTI );
				}
				$count ++;
			}
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		$this->_redirectCustom ( $cmspage );
	}
	
	public function uploadPhotoAction() {
		$cmspage = Engine_Api::_ ()->getItem ( 'cmspage', ( int ) $this->_getParam ( 'cmspage_id' ) );
		
		if (! $this->_helper->requireUser ()->checkRequire ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Max file size limit exceeded (probably).' );
			return;
		}
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid request method' );
			return;
		}
		
		// @todo check auth
		//$cmspage
		

		$values = $this->getRequest ()->getPost ();
		if (empty ( $values ['Filename'] )) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'No file' );
			return;
		}
		
		if (! isset ( $_FILES ['Filedata'] ) || ! is_uploaded_file ( $_FILES ['Filedata'] ['tmp_name'] )) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid Upload' );
			return;
		}
		
		$db = Engine_Api::_ ()->getDbtable ( 'photos', 'cmspage' )->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$viewer = Engine_Api::_ ()->user ()->getViewer ();
			$album = $cmspage->getSingletonAlbum ();
			
			$params = array (// We can set them now since only one album is allowed
			'collection_id' => $album->getIdentity (), 'album_id' => $album->getIdentity (), 

			'cmspage_id' => $cmspage->getIdentity (), 'user_id' => $viewer->getIdentity () );
			
			$photo = Engine_Api::_ ()->cmspage ()->createPhoto ( $params, $_FILES ['Filedata'] );
			
			$photo_id = $photo->photo_id;
			
			if (! $cmspage->photo_id) {
				$cmspage->photo_id = $photo->file_id;
				$cmspage->save ();
			}
			
			$this->view->status = true;
			$this->view->name = $_FILES ['Filedata'] ['name'];
			$this->view->photo_id = $photo_id;
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'An error occurred.' );
			// throw $e;
			return;
		}
	}
	
	public function viewAction() {
		$this->view->photo = $photo = Engine_Api::_ ()->core ()->getSubject ();
		$this->view->album = $album = $photo->getCollection ();
		$this->view->cmspage = $cmspage = $photo->getCmspage ();
		
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->canUpload = $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'photo' )->checkRequire ();
		
		$photo->view_count ++;
		$photo->save ();
	}
	
	public function editAction() {
		
		$photo = Engine_Api::_ ()->core ()->getSubject ();
		
		$this->view->cmspage = $cmspage = $photo->getCmspage ();
		if (! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'photo' )->isValid ())
			return;
		
		$this->view->form = $form = new Cmspage_Form_Photo_Edit ();
		
		if (! $this->getRequest ()->isPost ()) {
			$form->populate ( $photo->toArray () );
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		$db = Engine_Api::_ ()->getDbtable ( 'photos', 'cmspage' )->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$photo->setFromArray ( $form->getValues () )->save ();
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		return $this->_forward ( 'success', 'utility', 'core', array ('messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Changes saved' ) ), 'layout' => 'default-simple', 'parentRefresh' => true, 'closeSmoothbox' => true ) );
	}
	
	public function deleteAction() {
		$photo = Engine_Api::_ ()->core ()->getSubject ();
		$this->view->cmspage = $cmspage = $photo->getParent ( 'cmspage' );
		
		if (! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'photo' )->isValid ())
			return;
		
		$this->view->form = $form = new Cmspage_Form_Photo_Delete ();
		
		if (! $this->getRequest ()->isPost ()) {
			$form->populate ( $photo->toArray () );
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		$db = Engine_Api::_ ()->getDbtable ( 'photos', 'cmspage' )->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$photo->delete ();
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		$parentRedirect = Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array ('controller' => 'photo', 'action' => 'list', 'subject' => $cmspage->getGuid () ), 'cmspage_extended', true );
		
		return $this->_forward ( 'success', 'utility', 'core', array ('messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Photo deleted' ) ), 'layout' => 'default-simple', 'parentRedirect' => $parentRedirect, 'closeSmoothbox' => true ) );
	
	}

}